package com.gulimall.gulimallsearch.thread;

import java.util.concurrent.*;

/**
 * @author mayifan
 * @version 1.0
 * @date 2024-12-27 11:13
 *
 * 1）、继承 Thread
 * 2）、实现 Runnable 接口
 * 3）、实现 Callable 接口 + FutureTask （可以拿到返回结果，可以处理异常）
 * 4）、线程池
 */

public class TreadTest {
    // 方式一：初始化一个线程池
    public static ExecutorService executor = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        System.out.println("main方法开始。。。");
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getId());
            System.out.println("结果是：");
            return (10 / 0);
        }, executor).handle((res,thr)->{
           if(res!=null){
               return 2;
           }else return 0;
        });
        System.out.println("main方法结束。。。"+integerCompletableFuture.get());
//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println(Thread.currentThread().getId());
//            System.out.println("结果是：" + (10 / 2));
//        }, executor);
//        Thread01 thread01 = new Thread01();
//        thread01.start();
//        Runable01 runable01 = new Runable01();
//        new Thread(runable01).start();
//        FutureTask<Integer> integerFutureTask = new FutureTask<>(new Callable01());
//        new Thread(integerFutureTask).start();
//        Integer i = integerFutureTask.get();

    }
    public static class Callable01 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            System.out.println("Thread01"+Thread.currentThread().getId());
            System.out.println("结果是：");
            return (10/2);
        }
    }
    public static class Runable01 implements Runnable{
        @Override
        public void run() {
            System.out.println("Thread01"+Thread.currentThread().getId());
            System.out.println("结果是："+(10/2));
        }
    }
    public static class Thread01 extends Thread{
        @Override
        public void run() {
            System.out.println("Thread01"+Thread.currentThread().getId());
            System.out.println("结果是："+(10/2));
        }
    }
}
